<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Help;
use App\Models\Helps;
use Illuminate\Http\Request;

class HelpController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        return view('admin.article.help');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd'    => (string)$request->get('kwd',''),
                'type'    => (string)$request->get('type',0),
                'status'    => (string)$request->get('status',0),
            ];
            $lists = Helps::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = Helps::findOrFail($id);
            }
            $info = json_encode($info);
            return view('admin.article.help_update',compact('info','id'));
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Help $request
     * @return bool
     */
    public function update(Help $request)
    {
        try{
            $data = $request->all();
            if($data['id']){
                $info = Helps::findOrFail($data['id']);
                $info->update($data);
                return $this->writeJson(0,null,'修改信息成功');
            }else{
                unset($data['id']);
                Helps::create($data);
                return $this->writeJson(0,null,'新增信息成功');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(is_array($id)){
                $where['id'] = ['in',$id];
            }else{
                $where['id'] = $id;
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Helps::where($where)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
